package com.lovo.exception;

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class task {
    public static void main(String[] args) {
        int length = 4;
        int[] arr = new int[length];// 机选号码
        generate(arr);
        int[] arr1 = new int[length];// 用户输入
        for (int i = 0; i < 5; i++) {
            input(arr1);
            System.out.println("您本次输入：" + Arrays.toString(arr1));
            boolean falg = judge(arr, arr1);
            if (!falg) {
                break;
            }
        }
        System.out.println("正确答案是：" + Arrays.toString(arr));
    }

    //判断
    public static boolean judge(int[] arr, int[] arr1) {
        int a = 0;
        int b = 0;
        String result = "";
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr1.length; j++) {
                if (arr[i] == arr1[j] && i == j) {
                    a++;
                } else if (arr[i] == arr1[j]) {
                    b++;
                }
            }
        }
        result += a + "A" + b + "B";
        System.out.println(result);
        if (a == 4 && flag == true) {
            System.out.println("恭喜您全答对啦！");
            flag = false;
        }
        return flag;
    }

    //用户输入
    public static void input(int[] arr1) {
        Scanner sc = new Scanner(System.in);
        int num = 0; // 定义一个变量，来接收用户输入的值
        boolean a = true;
        for (int i = 0; i < arr1.length; i++) {
            try {
                System.out.println("请用户输入第" + (i + 1) + "个数：");
                num = sc.nextInt();
                arr1[i] = num;
                // 判断是否有重复数
                for (int j = 0; j < i; j++) {
                    if (arr1[i] == arr1[j]) { // 找到了重复数，跳过这次随机，并退回一次循环
                        i--;
                    }
                }
            } catch (InputMismatchException e) {
                System.out.println("输错了，请重新输入");
                i--;
                sc.next();
            } catch (Exception e) {
                System.out.println("未知异常" + e.getMessage());
                e.printStackTrace();
                i--;
                sc.next();
            }
        }
    }

    //生成机选号码
    public static void generate(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int num = random(9);
            boolean flag = true;
            // 判断是否有重复数
            for (int j = 0; j < arr.length; j++) {
                if (arr[j] == num) { // 找到了重复数，跳过这次随机，并退回一次循环，重新随机
                    flag = false;
                    i--;
                    break;
                }
            }
            if (flag) {
                arr[i] = num;
            }
        }
    }

    //随机数
    //1~9
    public static int random(int min, int max) {
        if (min > max) {
            int temp = max;
            max = min;
            min = temp;
        }
        return (int) (Math.random() * (max - min + 1) + min);
    }

    //0~9
    public static int random(int max) {
        return (int) (Math.random() * (max + 1));
    }
}
